﻿
        $(function () {
            //在左上角显示城市和县的列表
            $("#list").click(function () {
                $("#cityList").css("top", "34px").css("left", "2px").show();
                return false;
            });
            $("#closeCityList").click(function () {
                $("#cityList").hide();
                return false;
            });
            //点击城市名称回到初始的加载状态
            $("#HCity").click(function () {
                map.clearOverlays(); //先清除地图上原有的覆盖物
                loadOneCityAndCounty();

            });
            loadOneCityAndCounty();

            //点击终端 发送信息
            $("#send").click(function () {
                var phone = $("#phoneHidden").val();
                var msg = $("#sendMsg").val();
                var sendTimes = $("#sendTimes").val();
                if (msg == "" || msg.length < 3) {
                    alert("发送内容不能为空或太短");
                    return;
                }
                $.post("../BLL/TerminalAsh.ashx", { "sendaction": "sendMsg", "phone": phone, "sendMsg": msg, "sendTimes": sendTimes }, function (data, status) {
                    if (status == "success") {
                        if (data == "ok") {
                            alert("信息已经发出！");
                        }
                    }
                });
            });
        });

        //页面初始化的加载
        function loadOneCityAndCounty() {
            //获取一个县对象将县名填入左上角的列表框中
            $.post("../BLL/County.ashx", { "action": "GetOneCounty" }, function (data, status) {
                if (status == "success") {
                    var county = $.parseJSON(data);
                    $("#HCity").html(county.Name);
                    var lng = county.Lng;
                    var lat = county.Lat;
                    var selfCode = county.SelfCode;
                    createMap(lng, lat);
                    var point = new BMap.Point(lng, lat);
                    map.centerAndZoom(point, 12);
                    setMapEvent();
                    addMapControl();
                    getDataBoundary(county.Name);
                    loadDataTerminalOfCounty(selfCode);
                }
            });
        }


        //获取一个县的所有终端数据
        function loadDataTerminalOfCounty(selfCode) {
            $.post("../BLL/TerminalAsh.ashx", { "action": "GetTerminalListByRankLevel", "selfCode": selfCode }, function (data, status) {
                if (status == "success") {
                    var terminal = $.parseJSON(data);
                    window.terminal = terminal;
                    for (var i = 0; i < terminal.length; i++) {
                        var lng = terminal[i].Lng;
                        var lat = terminal[i].Lat;
                        var point = new BMap.Point(lng, lat);
                        addMarkerTerminal(point, i);
                    }
                }
                else {
                    alert("数据加载失败");
                }
            });
        }

        //添加终端的标注和事件
        function addMarkerTerminal(point, index) {
            var img = "";
            if (terminal[index].Statuss == "在线" && terminal[index].Category == "0") {
                img = "../images/green.png";
            } else if (terminal[index].Statuss == "离线" && terminal[index].Category == "0") {
                img = "../images/red.png";
            } else if (terminal[index].Statuss == "离线" && terminal[index].Category == "1") {
                img = "../images/icon7.gif";
            } else if (terminal[index].Statuss == "在线" && terminal[index].Category == "1") {
                img = "../images/icon6.gif";
            }
            var myIcon = new BMap.Icon(img, new BMap.Size(20, 31),
                                {
                                    anchor: new BMap.Size(10, 31), //标注的偏移，让标注点的下端指向经纬度坐标
                                    infoWindowAnchor: new BMap.Size(10, 0) //信息框的偏移  让信息框的下断点指向标注的上端
                                }
             );

            var marker = new BMap.Marker(point, { icon: myIcon });
            map.addOverlay(marker);

            //给标注添加鼠标滑过事件
            marker.addEventListener("mouseover", function () {
                //alert(mapDataArray[0]);
                var infoWindow = new BMap.InfoWindow();
                var detailInfow = '<div>所在地：' + terminal[index].TownName + '<br/>气象信息员：' + terminal[index].Leader + '</div>';
                infoWindow.setContent(detailInfow);
                this.openInfoWindow(infoWindow, map.getCenter());
            });

            marker.addEventListener("click", function () {
                // 弹出遮罩层的信息发送框
                var phone = terminal[index].Phone;
                $("#phoneHidden").val(phone);
                var villageName = terminal[index].TownName;
                $("#villageName").val(villageName);
                ShowDiv('MyDiv', 'fade');
            });
        }

        //通过传入的区域名称画出该区域的行政边界
        function getDataBoundary(distictName) {
            var bdary = new BMap.Boundary();
            bdary.get(distictName, function (rs) {       //获取行政区域
                //  map.clearOverlays();        //清除地图覆盖物       
                var count = rs.boundaries.length; //行政区域的点有多少个
                for (var i = 0; i < count; i++) {
                    var ply = new BMap.Polygon(rs.boundaries[i], { strokeWeight: 3, strokeColor: "#0000FF" }); //建立多边形覆盖物
                    ply.setFillColor();
                    map.addOverlay(ply);  //添加覆盖物
                    // map.setViewport(ply.getPath());    //调整视野         
                }
            });
        }
